﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ecobee.Net
{
	public class Weather : ApiObject
	{
		#region Private Fields
		private DateTime _timestamp = DateTime.MinValue;
		private GeoCode _weatherStation = GeoCode.Empty;
		private WeatherForecastList _forecast = new WeatherForecastList();
		#endregion

		[JsonIgnore]
		public override string UniqueKey
		{
			get
			{
				return this.Timestamp.Date.ToString();
			}
		}

		/// <summary>
		/// The time stamp in UTC of the weather forecast. 
		/// </summary>
		[JsonProperty(PropertyName = "timestamp")]
		public DateTime Timestamp
		{
			get
			{
				return _timestamp;
			}
			set
			{
				_timestamp= value;
			}
		}

		/// <summary>
		/// The weather station identifier. 
		/// </summary>
		[JsonProperty(PropertyName = "weatherStation")]
		public GeoCode WeatherStation
		{
			get
			{
				return _weatherStation;
			}
			set
			{
				_weatherStation= value;
			}
		}

		/// <summary>
		/// The list of latest weather station forecasts. 
		/// </summary>
		[JsonProperty(PropertyName = "forecasts")]
		public WeatherForecastList Forecast
		{
			get
			{
				return _forecast;
			}
			set
			{
				_forecast= value;
			}
		}

		protected override string OnDeserializeKeyValue(dynamic dataObject)
		{
			return ((JValue)dataObject.Timestamp).ToObject<DateTime>().Date.ToString();
		}

		public override void LoadFrom(dynamic dataObject)
		{
			this.Forecast.Clear();
			base.LoadFrom((object)dataObject);
		}
	}
}
